package com.aishua.fire.controller;

import java.util.List;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.addressBook.AddAddressBookInfo;
import com.aishua.fire.entity.addressBook.AddAddressBookInfos;
import com.aishua.fire.entity.addressBook.AddressBookListInfo;
import com.aishua.fire.entity.addressBook.AddressBookToOrganization;
import com.aishua.fire.service.AddressBookService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "通讯录管理",tags = {"通讯录管理模块接口"})
@RestController
@Slf4j
public class AddressBookManagerController
{

	@Autowired
	private AddressBookService bookService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.addressBookManager,moduleName=ModuleConstants.addressBookManager)
	@ApiOperation(value="通讯录管理_新增通讯录信息")
	@PostMapping(value = {"/addressbook_s"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody @Valid AddAddressBookInfo info, BindingResult result) throws ValidateException
	{
		log.info("通讯录管理_新增通讯录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = bookService.save(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	
	/**
	 * 通讯录管理_新增通讯录信息
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "通讯录管理_查询通讯录信息列表")
	@GetMapping(value = { "/addressbook" })
	public ResponseEntity<JsonArrayResult> list(
			@RequestParam(name = "name") String name, @RequestParam(name = "phone") String phone,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("查询列表请求参数:{}");
		
		List<AddressBookListInfo> books = bookService.list(name, phone, pageNumber, pageSize);
		int total = bookService.listTotal(name, phone);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(books, total);

		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.addressBookManager,moduleName=ModuleConstants.addressBookManager)
	@ApiOperation(value="通讯录管理_修改通讯录信息")
	@PutMapping(value = {"/addressbook"} )
	public ResponseEntity<JsonObjectResult> update(@RequestBody @Valid AddAddressBookInfo info, BindingResult result) throws ValidateException
	{
		log.info("通讯录管理_修改通讯录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = bookService.update(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.addressBookManager,moduleName=ModuleConstants.addressBookManager)
	@ApiOperation(value="通讯录管理_新增通讯录信息时，获取组织结构下拉框")
	@GetMapping(value = {"/addressbook/organization"} )
	public ResponseEntity<JsonObjectResult> selectOrganizationList()
	{
		log.info("通讯录管理_新增通讯录信息时，获取组织结构下拉框:{}");
		AddressBookToOrganization bean = bookService.selectOrganizationList();
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(bean);
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.addressBookManager,moduleName=ModuleConstants.addressBookManager)
	@ApiOperation(value="通讯录管理_新增通讯录信息(备用)")
	@PostMapping(value = {"/addressbook_old"} )
	public ResponseEntity<JsonObjectResult> add_s(@RequestBody @Valid AddAddressBookInfos info, BindingResult result) throws ValidateException
	{
		log.info("通讯录管理_新增通讯录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = bookService.save_s(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.addressBookManager,moduleName=ModuleConstants.addressBookManager)
	@ApiOperation(value="通讯录管理_删除通讯录信息")
	@DeleteMapping(value = {"/addressbook"} )
	public ResponseEntity<JsonObjectResult> delete(@RequestBody AddAddressBookInfo info) 
	{
		log.info("通讯录管理_删除通讯录信息 :{}",info.getId());
		bookService.delete(info.getId());
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }

}